/* ozofra 1b
 This file contains the complete definition
 of the MasterMind class. */

#include <iostream>
#include "code.h"

class MasterMind
{
 public:
  MasterMind (int length,  int range);
  MasterMind ();
  
  void playGame();

 private:
  SecretCode code;
  int codeLength;
  vector<int> guess;

  void getGuess();
  bool checkGuess (int &numCorrect, int &numIncorrect) const;
  void printResults(const int numCorrect, const int numIncorrect) const;
};

MasterMind::MasterMind ( int length,  int range)
:code(length, range)
{
  codeLength = length;
}

MasterMind::MasterMind ()
:code(5,10)
{
  codeLength = 5;
}

void MasterMind::playGame ()
// Keep on getting guesses and checking them until the player wins
// We catch exceptions here, but keep playing
{
  bool won = false;
  int numCorrect, numIncorrect;

  code.showAnswer();

  //Start playing until you win
  while (won == false)
  {
    //Get guess vector from user
    getGuess();

    //Catch any errors from input, but keep playing afterwards
    won = checkGuess(numCorrect, numIncorrect);
    printResults(numCorrect, numIncorrect);
  }

  printf ("You win!\n");

} //end playGame

bool MasterMind::checkGuess (int &numCorrect, int &numIncorrect) const
// checkGuess will return true if the player wins.
// Also returns the number of correct and incorrect items
// which will be passed to printResults.
// Called by playGame.
{
  //Check the guess, return results by reference
  numCorrect = code.checkCorrect(guess);
  numIncorrect = code.checkIncorrect(guess);

  //If every item in guess is correct, game is won
  if (numCorrect == codeLength)
    return true;
  else
    return false;

} //end checkGuess

  
void MasterMind::getGuess ()
// getGuess reads a vector of length codeLength from STDIN.
// Checks if guess is valid
// Called by playGame.
{
  printf("\nYour guess? (no spaces, please)\n");
  std::cin >> guess;

  if (guess.size() != codeLength)
    throw baseException("Bad guess!");

  //Check validity of each guess. Bad guess are <0 or >9
  for (int idx=0; idx<guess.size(); idx++)
  {
    if (guess.at(idx) < 0 || guess.at(idx) > 9)
      throw baseException("Bad guess!");
  }

} //end getGuess

void MasterMind::printResults (const int numCorrect, const int numIncorrect) const
// Prints the results of the turn. Called by playGame.
{
  printf ("Your result : (%d,%d)\n", numCorrect, numIncorrect);
}
